//require CameraKitCodenameOne
package com.codename1.samples;


import com.codename1.camerakit.CameraEvent;
import com.codename1.camerakit.CameraKit;
import com.codename1.camerakit.CameraKitFeatures;
import com.codename1.camerakit.CameraListener;
import com.codename1.components.FloatingActionButton;
import com.codename1.components.MediaPlayer;
import com.codename1.components.SpanLabel;
import com.codename1.components.ToastBar;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.media.Media;
import com.codename1.media.MediaManager;
import com.codename1.ui.Button;
import com.codename1.ui.Command;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.FontImage;
import com.codename1.ui.Image;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.LayeredLayout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class CameraKitSample {

    private Resources theme;
    private CameraKit ck;
    private Reel reel;
    private Form current;
    
    public void init(Object context) {
        ck = CameraKit.create();

        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

    }
    
    private class Reel extends Container {
        
        Reel() {
            super(BoxLayout.y());
            setScrollableY(true);
        }
        
        
        
        public void addToReel(Image img) {
            img = img.scaledWidth(isTablet() ? convertToPixels(50) : convertToPixels(20));
            add(new Label(img));
        }
        
        public void addVideo(String path) {
            try {
                Media m = MediaManager.createMedia(path, true);
                m.prepare();
                
                //m.setNativePlayerMode(true);
                Component videoCmp = m.getVideoComponent();
                videoCmp.setPreferredW(isTablet() ? convertToPixels(50) : convertToPixels(20));
                videoCmp.setPreferredH(isTablet() ? convertToPixels(30) : convertToPixels(12));
                
                add(videoCmp);
                
            } catch (Exception ex) {
                Log.e(ex);
                ToastBar.showErrorMessage("Failed to load video from path "+path);
            }
        }
    }
    
    public void start() {
        if(ck != null && !ck.isStarted()) {
            ck.start();
        }
        if(current != null){
            current.show();
            return;
        }
        reel = new Reel();
        Form hi = new Form("Native Camera", new LayeredLayout());
        hi.setScrollableY(false);
        if(ck != null) {
            ck.addCameraListener(new CameraListener() {
                @Override
                public void onError(CameraEvent ev) {
                    // We currently get some errors on Android
                    Log.p(ev.getMessage() + " : " + ev.getExceptionMessage());
                }

                @Override
                public void onImage(CameraEvent ev) {
                    ToastBar.showInfoMessage("Captured image bytes");
                    byte[] bytes = ev.getJpeg();
                    Image img = Image.createImage(bytes, 0, bytes.length);
                    reel.addToReel(img);
                    hi.revalidate();
                }

                @Override
                public void onVideo(CameraEvent ev) {
                    ToastBar.showInfoMessage("Captured video: " + ev.getFile());
                    reel.addVideo(ev.getFile());
                    hi.revalidate();
                }
            });
            hi.add(ck.getView());
            Button video = new Button();
            FontImage.setMaterialIcon(video, FontImage.MATERIAL_VIDEOCAM);
            video.addActionListener(e -> {
                if (!ck.supportsFeatures(CameraKitFeatures.CaptureVideo)) {
                    ToastBar.showErrorMessage("Video capture is not supported on this platform");
                    return;
                }
                Boolean b = (Boolean)video.getClientProperty("capturing");
                if(b == null) {
                    video.putClientProperty("capturing", Boolean.TRUE);
                    ck.captureVideo();
                    FontImage.setMaterialIcon(video, FontImage.MATERIAL_VIDEOCAM_OFF);
                } else {
                    video.putClientProperty("capturing", null);
                    ck.stopVideo();
                    FontImage.setMaterialIcon(video, FontImage.MATERIAL_VIDEOCAM);
                }
            });
            FloatingActionButton fab = FloatingActionButton.createFAB(FontImage.MATERIAL_CAMERA);
            fab.bindFabToContainer(hi, CENTER, BOTTOM);
            fab.addActionListener(e -> {
                if (ck.supportsFeatures(CameraKitFeatures.CaptureImage)) {
                    ck.captureImage();
                } else {
                    ToastBar.showErrorMessage("Image capture is not supported on this platform");
                }
                    
            });
            
            Button toggleCamera = new Button();
            FontImage.setMaterialIcon(toggleCamera, FontImage.MATERIAL_CAMERA_FRONT);
            Button toggleFlash = new Button();
            
            FontImage.setMaterialIcon(toggleFlash, FontImage.MATERIAL_FLASH_ON);
            toggleCamera.addActionListener(e -> {
                if (ck.supportsFeatures(CameraKitFeatures.ToggleFacing)) {
                    ck.toggleFacing();
                } else {
                    ToastBar.showErrorMessage("Front/Back camera toggle is not supported on this platform.");
                }
                    
            });
            toggleFlash.addActionListener(e -> {
                if (ck.supportsFeatures(CameraKitFeatures.Flash)) {
                    ck.toggleFlash();
                } else {
                    ToastBar.showErrorMessage("Flash is not supported on this platform");
                }
            });
            Container buttons = BoxLayout.encloseY(video, toggleCamera, toggleFlash);
            buttons.setScrollableY(true);
            hi.add(BorderLayout.east(buttons));
            
            hi.add(BorderLayout.west(reel));
        } else {
            hi.add(BorderLayout.north(new SpanLabel("Loading native camera view")));
        }
        hi.show();
    }

    public void stop() {
        if(ck.isStarted()) {
            ck.stop();
        }
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
